package ch.zbw.pra.sysventory.ui.gui.panels.scanpanel.subpanels;

import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import ch.zbw.pra.sysventory.domain.businesslogic.DomainController;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator;
import ch.zbw.pra.sysventory.domain.factories.FileGenerator.FileType;
import ch.zbw.pra.sysventory.ui.gui.utilities.DefaultFonts.FontType;
import ch.zbw.pra.sysventory.ui.gui.utilities.GuiLayout;
import ch.zbw.pra.sysventory.ui.gui.utilities.TextLayout;

public class ScanButtonPanel extends JPanel implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1942965669635856242L;
	private ImageIcon barcodeScannerImage;
	private JLabel barcodeScannerImageLabel;
	private JButton startScanButton;
	private JButton stopScanButton;
	private JLabel startScanTextLabel;
	private JLabel stoppScanTextLabel;
	private ProgressbarPanel progressBarPanel;

	public ScanButtonPanel(ProgressbarPanel progressBarPanel) {
		super();
		this.progressBarPanel = progressBarPanel;
		this.startScanButton = new JButton("Scan");
		this.startScanButton.addMouseListener(this);
		this.stopScanButton = new JButton("Stopp");
		this.stopScanButton.addMouseListener(this);
		this.barcodeScannerImage = new ImageIcon(new FileGenerator().getFile(FileType.BARCODE).getAbsolutePath());
		this.barcodeScannerImageLabel = new JLabel(this.barcodeScannerImage);
		this.startScanTextLabel = new JLabel("Um den Scanvorgang zu starten, klicken Sie auf Scan");
		this.stoppScanTextLabel = new JLabel("Um den Scanvorgang abzubrechen, klicken Sie auf Stopp");
		init();
	}

	public void init() {
		TextLayout.addStyleToComponent(this.startScanTextLabel, FontType.MEDIUM);
		TextLayout.addStyleToComponent(this.stoppScanTextLabel, FontType.MEDIUM);
		this.barcodeScannerImageLabel.setBorder(BorderFactory.createLineBorder(Color.black));
		JPanel placeholderPanel = new JPanel();
		// placeholderPanel.setBackground(Color.WHITE);
		GridBagLayout gridBagLayout = GuiLayout.getGridBagLayout(placeholderPanel);
		GridBagConstraints gridBagConstraints = GuiLayout.getGridBagConstraints();
		// gridBagLayout.rowWeights = new double[] { 0.0, 1.0 };
		gridBagConstraints.insets = new Insets(5, 5, 5, 5);
		gridBagLayout.columnWidths = new int[] { 150, 150 };
		int x = 0;
		int y = 0;

		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.startScanTextLabel);
		x = 0;
		y++;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.startScanButton);
		x = 0;
		y++;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.stoppScanTextLabel);
		x = 0;
		y++;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.stopScanButton);
		x = 0;
		y++;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, new JLabel());
		x = 0;
		y++;
		GuiLayout.addToLayout(placeholderPanel, gridBagLayout, gridBagConstraints, x, y++, this.barcodeScannerImageLabel);
		x = 0;
		y++;

		this.add(placeholderPanel);

	}

	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		if (e.getSource() == this.startScanButton) {

			this.progressBarPanel.startProgressBarSequenceOnProgressbarPanel();
			DomainController.getInstance().setScanStatus(true);
			System.out.println("Scanvorgang gestartet");
		}
		if (e.getSource() == this.stopScanButton) {
			this.progressBarPanel.stoppProgressBarThread();
			DomainController.getInstance().setScanStatus(false);
		}
	}
}
